<?php
/**
 * Created by PhpStorm.
 * User: wangyufei
 * Date: 2019/2/20
 * Time: 3:07 PM
 */

namespace App\Http\Controllers\Admin;


use App\Base\BaseFunc;
use App\Base\OrderStatus;
use App\Base\TaskState;
use App\Http\Controllers\Controller;
use App\Model\Accessory;
use App\Model\Task;
use Illuminate\Http\Request;
use App\Model\Service;

class ShopController extends Controller
{
    public function addService()
    {
        session(['now_address' =>'/addService']);
        return view('/admin.addService');
    }

    public function _addService(Request $request,BaseFunc $baseFunc)
    {
        Service::create([
            'service_name' => $request->input('service_name'),
            'service_shop' => session('shop_id'),
            'service_price' => $request->input('service_price'),
            'service_info' => $request->input('service_info'),
            'service_number' => 0
        ]);
        $baseFunc->setRedirectMessage(true,'添加成功',null,'/addService');
    }

    public function searchService()
    {
        $data['serviceData'] = Service::where('service_shop',session('shop_id'))->get();
        session(['now_address' =>'/searchService']);
        return view('/admin.searchService',$data);
    }

    public function delService($id,BaseFunc $baseFunc)
    {
        Service::destroy($id);
        $baseFunc->setRedirectMessage(true,'删除成功',null,'/searchService');
    }

    public function getTask(OrderStatus $orderStatus)
    {
        $status = $orderStatus->toOrder()->status_id;
        $data['taskData'] = Task::join('user','user_id','=','task_user')
            ->join('service','service_id','=','task_service')
            ->where('service_shop',session('shop_id'))
            ->where('task_status',$status)
            ->get();
        session(['now_address' =>'/getTask']);
        return view('/admin.getTask',$data);
    }

    public function toDoTask(OrderStatus $orderStatus,$id,BaseFunc $baseFunc)
    {
        $status['task_status'] = $orderStatus->unOrder()->status_id;
        Task::where('task_id',$id)->update($status);
        $file = Accessory::where('accessory_id',$id)->first();
        $fileName = public_path() . '/files/' . $file->accessory_address;
        $baseFunc->setRedirectMessage(true,'已接单',null,null);
        return response()->download($fileName);
    }

    public function tasking(OrderStatus $orderStatus)
    {
        $status = $orderStatus->unOrder()->status_id;
        $data['taskData'] = Task::join('user','user_id','=','task_user')
            ->join('service','service_id','=','task_service')
            ->where('service_shop',session('shop_id'))
            ->where('task_status',$status)
            ->get();
        session(['now_address' =>'/tasking']);
        return view('/admin.tasking',$data);
    }

    public function toTasked(OrderStatus $orderStatus,$id)
    {
        $status['task_status'] = $orderStatus->Ordering()->status_id;
        Task::where('task_id',$id)->update($status);
        return redirect()->back();
    }

    public function tasked(OrderStatus $orderStatus)
    {
        $status = $orderStatus->Ordering()->status_id;
        $data['taskData'] = Task::join('user','user_id','=','task_user')
            ->join('service','service_id','=','task_service')
            ->where('service_shop',session('shop_id'))
            ->where('task_status',$status)
            ->get();
        session(['now_address' =>'/tasked']);
        return view('/admin.tasked',$data);
    }

    public function endTask(OrderStatus $orderStatus)
    {
        $status = $orderStatus->end()->status_id;
        $data['taskData'] = Task::join('user','user_id','=','task_user')
            ->join('service','service_id','=','task_service')
            ->where('service_shop',session('shop_id'))
            ->where('task_status',$status)
            ->get();
        session(['now_address' =>'/endTask']);
        return view('/admin.endTask',$data);
    }
}